from PyQt5.QtWidgets import QMainWindow, QSplitter, QWidget, QVBoxLayout
from tab_manager import TabManager
from folder_tree import FolderTreeView
from color_table import ColorTable
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QT图色")
        self.resize(1200, 800)
        self.init_ui()
        
    def on_rect_selected(self, rect_params):
        # 将矩形参数传递给folder_tree显示
        self.folder_tree.display_rect_params(rect_params)

    def on_del_selected(self):
        # 将矩形参数传递给folder_tree显示
        self.folder_tree.clear_rect_params()    

    def on_color_selected(self, color, pos):
        # 将矩形参数传递给folder_tree显示
        self.color_table.addRow(color,pos)

    def init_ui(self):
        splitter = QSplitter()
        
        # 左侧文件夹树
        self.folder_tree = FolderTreeView()
        self.folder_tree.img_selected.connect(self.open_img)
        self.folder_tree.crop_req.connect(self.crop_img)
        self.folder_tree.save_req.connect(self.save_img)
        self.folder_tree.setMinimumWidth(200)
        self.folder_tree.setMaximumWidth(200)
        splitter.addWidget(self.folder_tree)

        # 右侧标签管理
        self.tab_manager = TabManager()
        self.tab_manager.rect_selected.connect(self.on_rect_selected)
        self.tab_manager.del_selected.connect(self.on_del_selected)
        self.tab_manager.color_selected.connect(self.on_color_selected)
        splitter.addWidget(self.tab_manager)

        self.color_table = ColorTable()
        self.color_table.setMinimumWidth(250)
        self.color_table.setMaximumWidth(250)
        splitter.addWidget(self.color_table)

        splitter.setStretchFactor(0, 1)
        splitter.setStretchFactor(1, 4)
        splitter.setStretchFactor(2, 1)

        container = QWidget()
        layout = QVBoxLayout(container)
        layout.addWidget(splitter)
        self.setCentralWidget(container)

    def open_img(self, img_path):
        self.tab_manager.open_img(img_path)

    def save_img(self):
        current_tab = self.tab_manager.currentWidget()
        if current_tab:
            current_tab.save_img()

    def crop_img(self):
        # 从当前标签页获取选中的矩形参数
        current_tab = self.tab_manager.currentWidget()
        if current_tab:
            cropped_pixmap = current_tab.crop_img()
            if cropped_pixmap:
                # 在新标签页中打开裁剪后的图像
                self.tab_manager.open_cropped_img(cropped_pixmap)
            

    
